<?php
namespace Home\Controller;

use Think\Controller;
use ThinkOauth;
class IndexController extends Controller {

    public function index() {
    
        $config = getTableConfig();
         $citys = getSub('area', $config['city']);
        if ($config['city'] > 0) {
            $word = "区域";
        } else {
            $word = "城市";
        }
        $type = I("get.type");
        if ($type == 'clean') {
//            cookie("area", null);
        } else {
            if (cookie("area") > 0) {
                $url = U("Area/lists", array("id" => cookie("area")));
                echo "<script>document.location.href='" . $url . "';</script>";
            }
        }
        $friends = getTableFile('friends');
        $this->assign("citys", $citys);
        $this->assign("word", $word);
        $this->assign("friends", $friends);
        $this->assign("config", $config);
        $this->display();
    }
    public function bind() {
//        echo cookie("area");;
        $this->assign("config", getTableConfig());
        $this->assign("threeLogin", getTableFile("accounts"));
        $this->display();
    }

    public function logout() {
        session('username', null);
        session('userid', null);
        cookie('userid', null);
        $url = C('SITE_URL');
        if (cookie("area") > 0) {
            $url = U("Area/lists", array("id" => cookie("area")));
        }
        echo "<script>document.location.href='" . $url . "';</script>";
    }

    public function login($type = null) {
        empty($type) && $this->error('参数错误');
        import('Org.ThinkSDK.ThinkOauth');


        $sns = ThinkOauth::getInstance($type);
//
//        //跳转到授权页面
        redirect($sns->getRequestCodeURL());
    }

     public function callback($type = null, $code = null) {
        (empty($type) || empty($code)) && $this->error('参数错误');
        import('Org.ThinkSDK.ThinkOauth');
        $sns = ThinkOauth::getInstance($type);

        //腾讯微博需传递的额外参数
        $extend = null;
        if ($type == 'tencent') {
            $extend = array('openid' => $this->_get('openid'), 'openkey' => $this->_get('openkey'));
        }
        $token = $sns->getAccessToken($code, $extend);
      

        session("openid", $token['openid']);
        $openid = session("openid");
//        print_r($openid);
//        exit;
        //获取当前登录用户信息
        if ($openid) {
            $Dao = M("user");
            $field = strtolower($type);
            $num = $Dao->field('id')->where("" . $field . "= " . $openid . "")->count();
            if ($num > 0) {
                $this->error("已被其他账户绑定！", $returnUrl);
                exit;
            }
            /*             * *******判断是否已登陆********** */
            $returnUrl = session('returnUrl');
            if ($returnUrl) {
                session('returnUrl', null);
            } else {
                $returnUrl = C("SITE_URL");
            }
            if (session('username') && session('userid')) {
                $info2 = $Dao->where("id= " . session('userid') . "")->find();
                if ($info2[$field] == '') {
                    $data[$field] = $openid;
                    $config = getTableConfig();
                    $points = $config["points_" . $field];
                    $Dao->where("id=" . session('userid') . "")->setInc('money', $points);
                    // echo $Dao->getlastsql();
                    addLog("bind_three", $points, session('userid'), "绑定" . $field . "获得" . $points . "积分", 5, 1);
                    session('type', null);
                    session('openid', null);
                    session("order", null);
                    $Dao->where("id = " . session('userid') . "")->save($data);
                    echo "<script>document.location.href='" . $returnUrl . "';</script>";
                } else {
                    $this->error("您的账号已绑定！", $returnUrl);
                    exit;
                }
            }
            /*             * *******判断是否已登陆over********** */
            $info = $Dao->field("id,name")->where("" . $field . "= '" . $openid . "'")->find();
//echo $Dao->getLastSql();exit;
            if ($info['id'] > 0) {

                session('username', $info['name']);
                session('userid', $info['id']);
                echo "<script>document.location.href='" . C("SITE_URL") . "';</script>";
            } else {
                $userinfo = A('Type', 'Event')->$type($token);
                session('threeName', $userinfo['name']);
                session('type', $field);
                $this->redirect("Index/bind");
            }
        }
    }

}